/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.DAO.MySQLDAO;

import common.DAO.ProductDAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import common.DAO.CategoryDAO;
import common.DAO.ComponentDAO;
import common.DAO.Factory.DAOFactory;

/** This is the data access object factory for mysql.
 * This class holds the driver, url, username and password for the database.
 * It also create connection function and getters for the DAOs
 * @author Richard Irons
 */
public class MySQLDAOFactory extends DAOFactory {
    //connection information
    /**
     * Driver for mysql
     */
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    /**
     * Location of the database
     */
    public static final String URL = "jdbc:mysql://tehserverz.ath.cx/cp3110";
            //
    //login to database info
    private static final String USERNAME = "root";
            //;
    private static final String PASSWORD = "fkd099";
            //;

    public static final String DBNAME = "cp3110";
            //;    

    /**
     * This function creates a connection to the database.
     * If an error occured the function will return null.
     * If there is no error this function will return the connection
     * @return
     * If an error occured, null else the connection
     */
    public static Connection createConnection(){
         Connection con = null;
         try{
             //initialise the driver class
            Class.forName(DRIVER);
            //connect to the database
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        }catch(Exception e){e.printStackTrace();return null;}
        return con;
    }
    public static boolean dbUpdate (String sql) {
        boolean status = false;
        try {
            Connection c = MySQLDAOFactory.createConnection();
            if (c != null) {
                Statement statment = c.createStatement();
                if ( statment.executeUpdate(sql) > 0 ) {
                    status = true;
                }
                c.close();
            }
            else {
                status = false;
            }
        } catch (Exception e){
            e.printStackTrace();
            status = false;
        }
        return status;
    }
    /**
     * Getter to create a ComponentDAOerDAO
     * @return
     * new MySQLComponentDAO
     */
    public CategoryDAO getCategoryDAO(){
        return new MySQLCategoryDAO();
    }

    public ComponentDAO getComponentDAO() {
        return new MySQLComponentDAO();
    }

    @Override
    public ProductDAO getProductDAO() {
        return new MYSQLProductDAO();
    }
}
